﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DimensionalMeasurement
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }
        private void bClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
#if DEBUG
            cUser.Text = "ME";
            tPassWord.Text = "123";
            return;
#endif


            try
            {
                if (PublicSet.Myset.SwipingCardLogin)
                {
                    string SwipingCardN, SwipingCardP;
                    if (PublicSet.Myset.PositiveSwitch)
                    {
                        SwipingCardP = FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginVerifyP);
                        if (SwipingCardP == "True")
                        {
                            Tool.LogSystemRuning("1.收到有效的刷卡登录信号");
                            PublicSet.userInfo.name = FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginUserName);
                            PublicSet.userInfo.power = (Power)int.Parse(FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginUserPower));
                            DialogResult = DialogResult.OK;
                        }
                    }
                    else if (PublicSet.Myset.NegativeSwitch)
                    {
                        SwipingCardN = FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginVerifyN);
                        if (SwipingCardN == "True")
                        {
                            Tool.LogSystemRuning("1.收到有效的刷卡登录信号");
                            PublicSet.userInfo.name = FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginUserName);
                            PublicSet.userInfo.power = (Power)int.Parse(FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginUserPower));
                            DialogResult = DialogResult.OK;
                        }
                    }
                    else
                    {
                        SwipingCardN = FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginVerifyN);
                        SwipingCardP = FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginVerifyP);
                        if (SwipingCardP == "True" || SwipingCardN == "True")
                        {
                            Tool.LogSystemRuning("1.收到有效的刷卡登录信号");
                            PublicSet.userInfo.name = FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginUserName);
                            PublicSet.userInfo.power = (Power)int.Parse(FormMain.instance.ReadVariable(FormMain.instance.nxCompoletWrite, PublicSet.Myset.LoginUserPower));
                            DialogResult = DialogResult.OK;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tool.LogForm("刷卡登录失败" + ex.ToString());
            }


            try
            {
                string[] userNames = QueryUserNames();
                //所有用户名录入用户选择下拉框
                cUser.Items.Clear();
                foreach (string item in userNames)
                {
                    cUser.Items.Add(item);
                }
                //选中第一个用户
                cUser.SelectedIndex = 0;
                //密码输入框置为空值
                tPassWord.Text = "";
                //聚焦密码输入框
                tPassWord.Focus();
            }
            catch (Exception)
            {
            }
        }

        private void pTitle_Paint(object sender, PaintEventArgs e)
        {
            Tool.FormMove(Handle);
        }

        private void cUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cUser.SelectedIndex != 0)
            {
                tPassWord.Text = "";
            }
            else
            {
                tPassWord.Text = "123";
            }
        }

        private void bLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tPassWord.Text.Trim()))
            {
                MessageBox.Show("密码不能为空！");
                tPassWord.Focus();
            }
            else
            {
                UserInfo userInfo;
                if (cUser.Text == "格林晟" && tPassWord.Text == "greensun")
                {
                    PublicSet.userInfo.name = "格林晟";
                    PublicSet.userInfo.password = "greensun";
                    PublicSet.userInfo.power = Power.开发者权限;
                    DialogResult = DialogResult.OK;
                    return;
                }
                //开始登录
                userInfo = QueryUserInfo(cUser.Text, tPassWord.Text);
                if (userInfo.name != null)
                {
                    PublicSet.userInfo = userInfo;
                    DialogResult = DialogResult.OK;
                }
                else if (cUser.Text == "格林晟" && tPassWord.Text == "greensun")
                {
                    PublicSet.userInfo.name = "格林晟";
                    PublicSet.userInfo.password = "greensun";
                    PublicSet.userInfo.power = Power.开发者权限;
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
            }

        }
        /// <summary>
        /// 查询所有用户名
        /// </summary>
        /// <returns>所有用户名</returns>
        public static string[] QueryUserNames()
        {
            try
            {
                DataTable dt = MySQLHelper.Select(PublicSet.Connection, "SELECT name FROM tb_user order by id ");
                string[] userNames = new string[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; ++i)
                {
                    userNames[i] = dt.Rows[i]["name"].ToString();
                }
                return userNames;
            }
            catch (Exception ex)
            {
                Tool.LogForm("查询用户失败。" + ex.ToString());
                return null;
            }


        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="name">查询用户的姓名</param>
        /// <param name="password">查询用户的密码</param>
        /// <returns>查询的用户</returns>
        public static UserInfo QueryUserInfo(string name, string password)
        {
            UserInfo userInfo = new UserInfo();
            DataTable dt = MySQLHelper.Select(PublicSet.Connection, $"SELECT * FROM tb_user WHERE name='{name}' AND password='{password}';");
            if (dt.Rows.Count == 1)
            {
                userInfo.name = dt.Rows[0]["name"].ToString();
                userInfo.password = dt.Rows[0]["password"].ToString();
                userInfo.power = (Power)dt.Rows[0]["power"];
            }
            return userInfo;
        }
    }
}
